package com.inter.law.action;

import com.inter.law.dao.IlDeptDAO;
import com.inter.law.dao.IlLawerDAO;
import com.inter.law.dao.IlLawerToClientDAO;
import com.inter.law.dao.IlLawerToContrayClientDAO;
import com.inter.law.entity.IlLawer;
import com.inter.law.entity.IlLawerToClient;
import com.inter.law.entity.IlLawerToContrayClient;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GetSomeones extends ActionSupport{
	private ApplicationContext ctx = new ClassPathXmlApplicationContext(
			"applicationContext.xml");
	private String name;
	private String json;
	private Boolean manager;
	 Map<String, Object> session = ActionContext.getContext().getSession();
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		IlLawerDAO ilLawerDAO =IlLawerDAO.getFromApplicationContext(ctx);
		if(manager!=null){
			System.out.println(manager+"");
		}
		System.out.println("hello:"+name+":"+manager);		
		List<IlLawer> list=ilLawerDAO.findByNameAndManager(name, manager, 1, 10);
		if(name!=null&&name.length()>0){
			String nameString=name.toLowerCase();
			if(name.charAt(0)>=97&&name.charAt(0)<=122){
				list=ilLawerDAO.findByNameAndPinyin(nameString, manager, 1, 10);
				JSONArray jsonArray=new JSONArray();
				if(list!=null&&list.size()>0){
					System.out.println("SSSS");
					IlDeptDAO ilDeptDAO=IlDeptDAO.getFromApplicationContext(ctx);
					for(int i=0;i<list.size();i++){
//						lawerIds.add(list.get(i).getLawerId());
//						lawerNames.add(list.get(i).getLawerName());
//						lawerDeptIds.add(list.get(i).getLawerDeptId());
						JSONObject jsonObject=new JSONObject();
						jsonObject.put("label",list.get(i).getLawerName()+"  "+list.get(i).getLawerPinyinName());
						jsonObject.put("name", list.get(i).getLawerName());
						jsonObject.put("id", list.get(i).getLawerId());
						jsonObject.put("deptname", ilDeptDAO.findNameById(list.get(i).getLawerDeptId()));
						jsonArray.add(jsonObject);
					}
				}
				
				
				json=jsonArray.toString();
				System.out.println(json);
				
				return SUCCESS;
			}
		}
		JSONArray jsonArray=new JSONArray();
		if(list!=null&&list.size()>0){
			System.out.println("SSSS");
			IlDeptDAO ilDeptDAO=IlDeptDAO.getFromApplicationContext(ctx);
			for(int i=0;i<list.size();i++){
//				lawerIds.add(list.get(i).getLawerId());
//				lawerNames.add(list.get(i).getLawerName());
//				lawerDeptIds.add(list.get(i).getLawerDeptId());
				JSONObject jsonObject=new JSONObject();
				jsonObject.put("label",list.get(i).getLawerName());
				jsonObject.put("name", list.get(i).getLawerName());
				jsonObject.put("id", list.get(i).getLawerId());
				jsonObject.put("deptname", ilDeptDAO.findNameById(list.get(i).getLawerDeptId()));
				jsonArray.add(jsonObject);
			}
		}
		
		
		json=jsonArray.toString();
		System.out.println(json);
		
		return SUCCESS;
		
	}
	
	public String executeClient() throws Exception {
		System.out.println("SDFFFFF");
		IlLawerToClientDAO ilLawerToClientDAO=IlLawerToClientDAO.getFromApplicationContext(ctx);
		String username=(String) session.get("username");
		IlLawerDAO ilLawerDAO=IlLawerDAO.getFromApplicationContext(ctx);
		ArrayList lists=(ArrayList) ilLawerDAO.findByLawerUsername(username);
		Integer id=0;
		if(lists!=null&&lists.size()>0){
			id=((IlLawer)(lists.get(0))).getLawerId();
		}
		if(id!=0){
			List<IlLawerToClient> list=ilLawerToClientDAO.findByLawerId(id);
			
			JSONArray jsonArray=new JSONArray();
			if(list!=null&&list.size()>0){
				
				for(int i=0;i<list.size();i++){
//					lawerIds.add(list.get(i).getLawerId());
//					lawerNames.add(list.get(i).getLawerName());
//					lawerDeptIds.add(list.get(i).getLawerDeptId());
					JSONObject jsonObject=new JSONObject();
					jsonObject.put("label", list.get(i).getName());
					jsonArray.add(jsonObject);
				}
			}	
			json=jsonArray.toString();
		}
		
		return SUCCESS;
	}
	
	public String executeContrayClient() throws Exception {
		IlLawerToContrayClientDAO ilLawerToContrayClientDAO=IlLawerToContrayClientDAO.getFromApplicationContext(ctx);
		String username=(String) session.get("username");
		IlLawerDAO ilLawerDAO=IlLawerDAO.getFromApplicationContext(ctx);
		ArrayList lists=(ArrayList) ilLawerDAO.findByLawerUsername(username);
		Integer id=0;
		if(lists!=null&&lists.size()>0){
			id=((IlLawer)(lists.get(0))).getLawerId();
		}
		if(id!=0){
		List<IlLawerToContrayClient> list=ilLawerToContrayClientDAO.findByLawerId(id);
		JSONArray jsonArray=new JSONArray();
		if(list!=null&&list.size()>0){
			
			for(int i=0;i<list.size();i++){
//				lawerIds.add(list.get(i).getLawerId());
//				lawerNames.add(list.get(i).getLawerName());
//				lawerDeptIds.add(list.get(i).getLawerDeptId());
				JSONObject jsonObject=new JSONObject();
				jsonObject.put("label", list.get(i).getName());
				jsonArray.add(jsonObject);
			}
		}	
		json=jsonArray.toString();
		}
		return SUCCESS;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

	public Boolean getManager() {
		return manager;
	}

	public void setManager(Boolean manager) {
		this.manager = manager;
	}
	
	

}
